package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPriceCompetitionBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.swgk.core.util.PickSelectEntity;

import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.viewmodel.home.PriceCompetitionModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * Description:
 * Author cfk
 * Time
 * 招标竞价中的竞价fragment
 */
public class PriceCompetitionFragment extends AppLazyFragment {

    private FragmentPriceCompetitionBinding binding;
    private MainActivity activity;
    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    @Inject
    PriceCompetitionModel viewModel;
    private PickBidingAdapter pickingAdapter;
    //类型
    private String typeNo;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }
    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_price_competition, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        super.initView();
        if (pickingAdapter == null) {
            pickingAdapter = new PickBidingAdapter(getActivity(), null);
        }

        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide1));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(pickingAdapter);
        binding.tvMoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.getBiddingList(typeNo,true);
            }
        });
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                typeNo = typeDatas.get(tab.getPosition()).getNo();
                initFilterData();
                pickingAdapter.setType(typeNo);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewModel.getFilterTypeData();
    }

    @Override
    protected void onLazyLoad() {
        initFilterData();
    }

    @Override
    public void onInvisible() {
        super.onInvisible();
        onLazyLoad();
    }

    private void initFilterData() {
        viewModel.getBiddingList( typeNo,false);
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        typeDatas.clear();
        typeDatas.addAll(datas);
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeDatas.get(i).getProjectscale()));
        }
        initFilterData();
    }

    public void showList(List<BiddingEntity> list, boolean isLast) {
        if (list.size() > 0) {
            pickingAdapter.setDatas(list);
        } else {
            pickingAdapter.setDatas(null);
        }
        if (list.size() <= 2){
            binding.tvMoreData.setVisibility(View.GONE);
        }else{
            binding.tvMoreData.setVisibility(View.VISIBLE);
        }

        pickingAdapter.setOnItemClick(new AdapterOnItemClick<BiddingEntity>() {
            @Override
            public void onItemClick(BiddingEntity tenderListEntity, int position) {
                viewModel.getUrlSetting(tenderListEntity.getId());
            }
        });
        pickingAdapter.onIChildClick(new PickBidingAdapter.IChildClick() {
            @Override
            public void onClick(BiddingEntity tenderListEntity) {
                viewModel.getUrlSetting(tenderListEntity.getId());
            }
        });
    }

    public void lodMore(List<BiddingEntity> list, boolean isLast) {
        if (list.size() > 0) {
            pickingAdapter.addDatas(list);
        }
        if (list.size() <= 2){
            binding.tvMoreData.setVisibility(View.GONE);
        }else{
            binding.tvMoreData.setVisibility(View.VISIBLE);
        }
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

}
